package com.example.qimo.conf;

import cn.hutool.json.JSONUtil;
import com.example.qimo.commom.Result;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;

import java.io.IOException;

public class MyLoginFilter extends FormAuthenticationFilter {
    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(JSONUtil.toJsonStr(Result.error("未登录")));
        response.getWriter().flush();
        return false;
    }


//    @Override
//    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue){
//        Subject subject =this.getSubject(request,response);
//        HttpServletRequest httpRequest = (HttpServletRequest) request;
//        String  reguestUrl  = httpRequest.getRequestURI();
//        String[] arr =  reguestUrl .split("/");
//            if(!subject.isPermitted(arr[1])) {
//            response.setContentType("application/json;charset=UTF-8");
//            try {
//                response.getWriter().write(JSONUtil.toJsonStr(new Result(501,"无法访问" ,"no-auth")));
//                return false;
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//        }
//                return true;
//    }







}

